package com.homwork.Collection_learn.List;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;

public class Exercise3 {
    public static void main(String[] args) {
        ArrayList<Book> list = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            list.add(new Book("红楼梦", "gqm", 10.0));
            list.add(new Book("红楼梦", "gqm", 2.0));
            list.add(new Book("红楼梦", "gqm", 1.0));
            list.add(new Book("红楼梦", "gqm", 100.0));
        }
        extracted(list);
        //，冒泡排序
        //交换两个对象的位置，不要直接操作对象的内存地址
        for (int i = 0; i < list.size()-1; i++) {
            for (int j = 0 ; j < list.size() - 1 - i; j++) {
                Book book;//用于存储的地址
                if (list.get(j).getPrice() > list.get(j+1).getPrice()) {
                    book = list.get(j);//将地址存储到book中
                        list.set(j,list.get(j+1));
                        list.set(j+1,book);
                }

            }
        }
        //输出
        System.out.println("输出");
        extracted(list);

    }

    private static void extracted(List<Book> List) {
        for (Book s : List) {
            System.out.println(s);
        }
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Book {
    private String name;
    private String author;
    private double price;

    @Override
    public String toString() {
        return "名称    " + name + "      " +
                "价格:   " + price + "      " +
                "作者：   " + author;
    }
}
